#include "Monitor.h"
#include "../TinyXML/tinyxml.h"
#include "../ChineseEnglishMapping.h"
#include <QDebug>
#include <QVBoxLayout>
#include <QSplitter>

Monitor::Monitor()
{

}

void Monitor::InitUI(const QString &fileName, QList<QPair<QWidget *, QString> > &widgets, QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    QWidget* widget = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(widget);

    widgets.append(qMakePair(widget,QStringLiteral("tab0")));

    QList<Monitorstruct> _MonitorstructList;
    Analysis(filePath, _MonitorstructList);

    TableWidgetForm* _TableWidgetForm = new TableWidgetForm(widget);
    layout->addWidget(_TableWidgetForm);
    _TableWidgetForm->setGroupBoxTitle(fileName.left(fileName.size()-4));

    //获取表头
    Monitorstruct _Monitorstructtemp;
    _TableWidgetForm->setTableWidgetHeaderLabels(COMMONS->getTableWidgetHeadLabels(fileName,_Monitorstructtemp.toFieldNames().split(',')));
    _TableWidgetForm->setTableWidgetHeaderType(_Monitorstructtemp.toFieldTypes());

    for (int j = 0; j < _MonitorstructList.size(); j++)
    {
        int rowCount = _TableWidgetForm->getTableWidget()->rowCount();
        _TableWidgetForm->getTableWidget()->setRowCount(rowCount+1);
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 0, new QTableWidgetItem(_MonitorstructList.at(j).name));
        _TableWidgetForm->getTableWidget()->setItem(rowCount, 1, new QTableWidgetItem(QString("%1").arg(_MonitorstructList.at(j).show_group)));
    }

    m_pTableWidgetFormMap.insert(0, QList<TableWidgetForm *>() << _TableWidgetForm);
}

bool Monitor::SaveUI(const QString &fileName, const QMap<int, QList<TableWidgetForm *> > &m_pTableWidgetFormMap)
{
    QString filePath = QString("%1/config/%2/%3")
            .arg(COMMONS->XmlPath)
            .arg(COMMONS->MachType)
            .arg(fileName);
    if(m_pTableWidgetFormMap.size() > 0 && m_pTableWidgetFormMap.value(0).size() > 0)
    {
        TableWidgetForm* _TableWidgetForm = m_pTableWidgetFormMap.value(0).at(0);
        if(_TableWidgetForm)
        {
            QList<Monitorstruct> _MonitorstructList;
            for(int k = 0;k<_TableWidgetForm->getTableWidget()->rowCount();k++)
            {
                Monitorstruct _Monitorstruct;
                _Monitorstruct.name = _TableWidgetForm->getTableWidget()->item(k,0)->text();
                _Monitorstruct.show_group = _TableWidgetForm->getTableWidget()->item(k,1)->text().toInt();
                _MonitorstructList.append(_Monitorstruct);
            }
            SaveXml(filePath, _MonitorstructList);
            return true;
        }
    }
    return false;
}

void Monitor::Analysis(const QString &path, QList<Monitorstruct> &_MonitorstructList)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    TiXmlElement* rootElement = xmlDocument->FirstChildElement("Monitors");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return;
    }

    if(rootElement)
    {
        for(TiXmlElement* childElement = rootElement->FirstChildElement("Monitor");
            childElement != nullptr; childElement = childElement->NextSiblingElement("Monitor"))
        {
            Monitorstruct _Monitorstruct;
            _Monitorstruct.name = childElement->Attribute("name");
            childElement->QueryIntAttribute("show_group", &_Monitorstruct.show_group);
            _MonitorstructList.append(_Monitorstruct);
        }
    }

    delete xmlDocument;
}

bool Monitor::SaveXml(const QString &path, const QList<Monitorstruct> &_MonitorstructList)
{
    TiXmlDocument* xmlDocument = new TiXmlDocument();
    if(!xmlDocument->LoadFile(path.toStdString().c_str()))
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return false;
    }

    TiXmlElement* rootElement = xmlDocument->FirstChildElement("Monitors");
    if(rootElement == nullptr) //节点不存在
    {
        qDebug() << xmlDocument->ErrorDesc();
        delete xmlDocument;
        return false;
    }

    if(rootElement)
    {
        for(TiXmlElement* childElement = rootElement->FirstChildElement("Monitor");
            childElement != nullptr; childElement = childElement->NextSiblingElement("Monitor"))
        {
            rootElement->RemoveChild(childElement);
        }
        for (auto _Monitorstruct : _MonitorstructList)
        {
            //元素类
            TiXmlElement* ChildxmlElement = new TiXmlElement("Monitor");
            ChildxmlElement->SetAttribute("name", _Monitorstruct.name.toStdString().c_str());
            ChildxmlElement->SetAttribute("show_group", _Monitorstruct.show_group);
            rootElement->LinkEndChild(ChildxmlElement);
        }
    }

    //保存
    xmlDocument->SaveFile(path.toStdString().c_str());

    delete xmlDocument;
    return true;
}
